Welcome to Error Handling. After watching this video, you will be able to Describe different HTTP status that API services can return Explain how error handling works in Flask And explain how to return errors from your API endpoints Every HTTP response contains a three-digit code Indicating different error and success status And the client is responsible for consuming this error code Valid response code ranges from 100 to 599 The error codes are organized in categories of 100 each Error codes from 100 to 199 Indicate the request has been received and are informational Error codes from 200 to 299 Indicate the request has been received And the requested operation is successful Error codes in the 300s Indicate there is a redirection on the server Error codes in the range from 400 to 499 Show an error in the request And finally, error codes from 500 to 599 Indicate an error on the server side The APIs you will write in this course will follow this nomenclature For example, if the client sends a GET request for an unavailable resource You can send a 404 response back And you can respond with a status 400 for incorrect requests Flask Server automatically returns a 200 OK status When you return from the at app.route method 200 is also returned by default when you use the jsonify method to respond to a request A successful response with the status code of 200 will be sent back when the given code executes Now your code can return a different code than the default one Flask allows you to send the status code in a tuple along with the response In the code, you send back an HTML response of My first application in action with the status code of 200 You can also use the make_response method to explicitly set the status code This code returns the same HTML message and HTTP status code of 200 as the previous code But here you use the make_response method Let's look at a few more examples of codes you might use in this course The status of 200 is returned by default It indicates that the request is successful 201 tells the client that the server has successfully created the resource 202 indicates that the request is accepted and is in the process and is common for batch processing operations The server returns 204 after completing a request successfully, no content is returned And this status is useful in cases where you don't want the browser to take any action For example, the user stays on the current page 400 indicates an invalid request This status could imply the parameters are missing or improper or the request is invalid in another way 401 indicates the credentials are missing or invalid 403 implies that the client credentials are not sufficient to fulfill the request And if the server is unable to find the resource, it returns a 404 status 405 indicates that the requested operation is not supported And 500 is used when there is an error on the server So, now that you know the different HTTP codes, you, as a developer, need to return the right code from the service Let's look at an example This search response method looks for the database's query parameter, q The service calls the mock FetchFromDatabase method after parsing your query The code returns the resource to the client if the resource exists Server returns a code of 200 implicitly and returns 404 if the resource is not found Now let's call this endpoint using the curl program Call the route without a query parameter The curl program returns a message of input parameter missing with a return status of 422 Next, call the route with the correct resource ID The curl command returns the resource as the body and the status of 200 Finally, call the route with a resource that does not exist The curl command returns a message of resource not found with a status of 404 Flask provides a way to handle error messages at the application level Here we see a method that processes the 404 error and returns a message API not found with a status code of 404 Similarly, this snippet creates an error handler for 500 errors and returns a message of something went wrong on the server In this video you learned that HTTP response requires a status code to indicate what happens when the request processes There are multiple classes of HTTP status codes showing success, user error, or server error Flask implicitly returns a success code of 200 with the response You can provide status codes explicitly and Flask provides application level error handlers